import 'package:flutter/material.dart';
import 'package:flutter_shop/model/category_page_data.dart';
import 'package:flutter_shop/model/goods_list.dart';
import 'package:flutter_shop/model/goods_list_model.dart';


class GoodsProvider with ChangeNotifier {
  List<GoodsData> goodsList;

  static String _grade1Id = '';
  String _grade2Id = '';

  String get grade2Id => _grade2Id;

  setGoodsList(List<GoodsData> list) {
    this.goodsList = list;
    notifyListeners();
  }

  // navElement 当前选中的二级类目对象
  // id 选中的一级类目的id
  getGoodsList({String id, subId}) {
    if (id == null && _grade1Id == '') id = '0001';
    if (id == null && _grade1Id != '') id = _grade1Id;
    subId ??= '';

    _grade1Id = id ?? '';
    _grade2Id = subId ?? '';

    id = id + subId;

    var _element = CategoryData.where((element) {
      return element['mallCategoryId'] == id;
    });

    if (_element != null) {
      // String grad1Id = _element.toList()[0]['mallCategoryId'];
      GoodsDataList _goodsList = GoodsDataList.fromJson(GoodsList);
      List<GoodsData> _goodsFilterList = _goodsList.list.where((e) => e.id.startsWith(id)).toList();

      setGoodsList(_goodsFilterList);
    }
  }
}